package com.bgs.engineering.api.syslog.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bgs.engineering.api.syslog.mapper.SysLogMapper;
import com.bgs.engineering.api.syslog.service.SysLogService;
import com.bgs.engineering.util.BeanUtils;
import com.bgs.engineering.util.ObjectUtils;
import com.bgs.engineering.util.pageUtil.MybatisPageHelper;
import com.bgs.engineering.util.pageUtil.PageRequest;
import com.bgs.engineering.util.pageUtil.PageResult;
import com.bgs.engineering.util.validator.ValidationResult;
import com.bgs.engineering.util.validator.ValidatorUtil;
import com.bgs.engineering.api.syslog.entity.SysLog;
import com.bgs.engineering.api.syslog.model.SysLogModel;
import com.bgs.engineering.common.errorCode.CommonException;
import com.bgs.engineering.common.errorCode.EmErrorCode;
import com.bgs.engineering.common.response.CommonResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @auther zcy
 * @data 2023/3/22 13:35
 */
@Service
public class SysLogServiceImpl extends ServiceImpl<SysLogMapper, SysLog> implements SysLogService {

  @Resource
  SysLogMapper sysLogMapper;

  /**
   * 分页查询
   *
   * @param pageRequest
   * @param sysLogModel
   * @return
   */
  @Override
  public PageResult selectSysLogByPage(PageRequest pageRequest, SysLogModel sysLogModel) {
    MybatisPageHelper.startPage(pageRequest);
    return MybatisPageHelper.getPageResult(sysLogMapper.selectSysLogByPage(sysLogModel));
  }

  /**
   * 删除日志
   *
   * @param id
   * @return
   * @throws CommonException
   */
  @Override
  public CommonResult deleteSysLogById(Integer id) throws CommonException {
    // 判断是否id为空
    if (ObjectUtils.isNotEmpty(id)) {
      int i = sysLogMapper.deleteById(id);
      // 判断是否操作成功
      if (i <= 0) {
        throw new CommonException(EmErrorCode.DELETE_ERROR);
      }
    } else {
      throw new CommonException(EmErrorCode.ID_NULL);
    }
    return CommonResult.ok(EmErrorCode.DELETE_OK);
  }

  @Override
  public SysLog selectSysLogById(Integer id) throws CommonException {
    // 判断是否id为空
    if (ObjectUtils.isNotEmpty(id)) {
      SysLog sysLog = sysLogMapper.selectById(id);
      // 判断是否操作成功
      if (ObjectUtils.isNotEmpty(sysLog)) {
        return sysLog;
      } else {
        throw new CommonException(EmErrorCode.ID_NOT_OBJECT);
      }
    } else {
      throw new CommonException(EmErrorCode.ID_NULL);
    }
  }

  /**
   * 根据时间段查询日志
   *
   * @param startTime
   * @param endTime
   * @return
   */
  @Override
  public Integer selectSysLogByTime(String startTime, String endTime) {
    Integer integer = sysLogMapper.selectSysLog(startTime, endTime);
    return integer;
  }

  /**
   * 获取用户时间范围内操作日志的次数
   *
   * @param startTime
   * @param endTime
   * @return
   */
  @Override
  public List<Map<String, Object>> getLogAnalysis(String startTime, String endTime) {
    List<Map<String, Object>> list = sysLogMapper.selectUserOperate(startTime, endTime);
    return list;
  }

  /**
   * 获取时间内所有用户不同ip的登录次数
   *
   * @param startTime
   * @param endTime
   * @return
   */
  @Override
  public List<Map<String, Object>> getLoginIpCount(String startTime, String endTime) {
    List<Map<String, Object>> loginIpcount = sysLogMapper.getLoginIpCount(startTime, endTime);
    return loginIpcount;
  }

  /**
   * 保存日志
   *
   * @param sysLogModel
   * @return
   * @throws CommonException
   */
  @Override
  public CommonResult saveSyslog(SysLogModel sysLogModel) throws CommonException {
    if (ObjectUtils.isNotEmpty(sysLogModel)) {
      ValidationResult result = ValidatorUtil.validate(sysLogModel);
      if (result.isHasError()) {
        throw new CommonException(EmErrorCode.PARAMETER_VALIDATION_ERROR, result.getErrMsg());
      }
      SysLog sysLog = new SysLog();
      // sysLogModel 对象拷贝成 sysLog对象中
      BeanUtils.copy(sysLogModel, sysLog);
      int i = sysLogMapper.insert(sysLog);
      if (i > 0) {
        return CommonResult.ok(EmErrorCode.SAVE_OK);
      } else {
        return CommonResult.ok(EmErrorCode.SAVE_ERROR);
      }
    } else {
      // 返回前端异常数据
      throw new CommonException(EmErrorCode.OBJECT_NOT_NULL);
    }
  }
}
